package com.rpp;

import com.rpp.boot.ProviderMain;
import com.rpp.utils.ZookeeperUtil;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Description
 * @Author rpp
 * @Date 2020/8/4 11:25 下午
 */
@SpringBootApplication
public class ServerBootStrap implements DisposableBean {

    //zookeeper客户端
    private static ZkClient zkClient;

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(ServerBootStrap.class, args);

        String ip = "127.0.0.1";
        int port = 8888;
        //创建zookeeper会话
        zkClient = ZookeeperUtil.createSession(ip, port);

        //启动服务器
        ProviderMain.startServer(ip, port);

        //将IP和端口信息注册到zookeeper，临时节点
        ZookeeperUtil.createNode(zkClient, ip, port);
    }

    /**
     * 重写bean销毁方法，方便在此处关闭zookeeper
     *
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        zkClient.close();
        System.out.println("关闭zookeeper连接。。。");
    }
}
